package com.dtf.admin.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dtf.admin.common.utils.CacheUtils;
import com.dtf.admin.common.utils.SerializeUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service("cacheUtils")
public class RedisUtils extends CacheUtils{
	
	@Autowired
	JedisPool jedisPool;
	
	private Jedis jedis;
	
	private Jedis getJedis(){
		if (jedis == null) {
			jedis = jedisPool.getResource();
		}
		return jedis;
	}
	
	@Override
	public String set(String key, Object value) {
		return getJedis().set(SerializeUtil.serialize(key), SerializeUtil.serialize(value));
	}

	@Override
	public String set(String key, Object value, int effTime) {
		return getJedis().setex(SerializeUtil.serialize(key), effTime, SerializeUtil.serialize(value));
	}

	@Override
	public Long incr(String key) {
		// TODO Auto-generated method stub
		return getJedis().incr(SerializeUtil.serialize(key));
	}

	@Override
	public Long incr(String key, long number) {
		// TODO Auto-generated method stub
		return getJedis().incrBy(SerializeUtil.serialize(key), number);
	}

	@Override
	public Long decr(String key) {
		// TODO Auto-generated method stub
		return getJedis().decr(SerializeUtil.serialize(key));
	}

	@Override
	public Long decr(String key, long number) {
		return getJedis().decrBy(SerializeUtil.serialize(key), number);
	}


	@Override
	public <E> E get(String key) {
		// TODO Auto-generated method stub
		return SerializeUtil.unserizlize(getJedis().get(SerializeUtil.serialize(key)));
	}


	@Override
	public Long del(String key) {
		return getJedis().del(key);
	}
	
}
